<?php


namespace app\api\controller;


use app\common\model\Member;
use think\Exception;
use think\facade\View;

class Base
{
//测试服务端
    public function testGet()
    {
        $test = get_param('test');
        echo '这是服务端收到的数据：key = > test' . '| value => 123' . $test;

    }

    public function login()
    {
        //数据效验
        $mobile = post_param('mobile');
        $password = post_param('password');
        if (empty($mobile) || empty($password)) output_error_param();
        //模型
        $memberModel = new Member();
        //判断用户名与密码是否匹配
        $condition = array();
        $condition[] = ['member_mobile', '=', $mobile];
        $condition[] = ['member_password', '=', md5_encrypt($mobile, $password)];
        $data = $memberModel->where($condition)->find();
        if (empty($data)) output_error('账号或密码错误~');
        //登录成功后的操作，生成token,存好，返回给用户
        $token = md5_encrypt(time(), $data['member_id']);
        //补全代码，更新数据库，输出token

        //更新数据
        $update = array();
        $update['member_token'] = $token;
        //执行更行语句
        $memberModel->where('member_id', $data['member_id'])->update($update);
        output_data($token);

    }
    //注册接口
    //要求：实现注册功能，需要验证重复用户
    public function register()
    {
        //实现步骤
        //第一步：验证数据是否有效，手机号，密码，姓名，年龄
        //第二步：查询该手机号是否已经存在，存在输出问题，不存在继续操作
        //第三步：加密密码入库
        $mobile = post_param('mobile');
        $password = post_param('password');
        if (empty($mobile) || empty($password)) output_error_param();
        $memberModel = new Member();
        $data = $memberModel->where('member_mobile', '=', $mobile)->find();
        if (!empty($data)) output_error('账号已存在');
        $data = array();
        $data['member_mobile'] = $mobile;
        $data['member_password'] = md5_encrypt($mobile, $password);
        $data['member_name'] = '用户' . $mobile;
        $data['add_time'] = time();
        if ($memberModel->insert($data)) output_success();
        output_error('注册失败');
    }


}